<template>
  <el-container class="home-container">
    <!-- 头部 -->
    <el-header>
      <div>
        <img src="../assets/logo.png" alt="登陆图标" />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出登陆</el-button>
    </el-header>

    <!-- 主体 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width=" isCollapse ? '64px' : '165px' ">
        <div class="toggle-button" @click="isCollapse = !isCollapse">|||</div>
        <!-- 菜单 -->
        <el-menu
          class="el-menu-vertical-demo"
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409eff"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          router
          :default-active="activePath">
          <!-- 左边列表 -->
          <el-submenu v-for="menu in MenuList" :key="menu.id" :index="menu.id+''">
      
            <template slot="title">
              <i :class="iconObj[menu.id]"></i>
              <span v-text="menu.authName"></span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item v-for="child in menu.children" :key="child.id" :index=" '/' + child.path " @click="saveNavState('/' + child.path)">
                <i class="el-icon-menu"></i>
                <span>{{child.authName}}</span>
            </el-menu-item>

          </el-submenu>

        </el-menu>
      </el-aside>
      <!-- 主要栏 -->
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>

    </el-container>
  </el-container>
</template>





<script>
export default {
  name: "home",
  data() {
    return {
      //左侧菜单数据
      MenuList: [],
      // icon对象
      iconObj:{
        '125':"iconfont icon-user",
        '103':"iconfont icon-tijikongjian",
        '101':"iconfont icon-shangpin",
        '102':"iconfont icon-danju",
        '145':"iconfont icon-baobiao",
      },
      isCollapse:true,
      // 被激活的链接地址
      activePath:""
    };
  },
  methods: {
    //退出登陆方法
    logout() {
      window.sessionStorage.clear();
      this.$router.push("/login");
      this.$message.error("退出成功！");
    },
    //获取所有左侧子菜单
    async getMenuList() {
      const { data: res } = await this.$axios.get("menus");
      if (res.meta.status != 200) {
        this.$message.error(res.meta.msg);
        console.log(res.meta.status);
        return;
      }
      this.MenuList = res.data;
    },
    // 列表链接激活状态
    saveNavState(activePath){
      this.activePath = activePath;
      window.sessionStorage.setItem('activePath',activePath);
    }
  },
  created() {
    //获取列表菜单
    this.getMenuList();
    //链接激活状态
    this.activePath = window.sessionStorage.getItem('activePath')
  }
};
</script>






<style lang="less" scoped>
.home-container {
  height: 100%;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  img {
    width: 60px;
  }
  div {
    display: flex;
    align-items: center;
  }
  span {
    font-size: 20px;
    color: rgb(212, 212, 212);
    margin-left: 15px;
  }
}
.el-aside {
  background-color: #333744;
  transition: 0.4s;
  .el-menu{
    border-right: none;
  }
}
.el-main {
  background-color: #eaedf1;
}
.iconfont{
  margin-right: 10px;
}
.toggle-button{
  background-color: #4A5064;
  color: white;
  font-size: 10px;
  line-height: 24px;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>